package com.gnerv.battle.boot.module.base.service.impl;

import com.gnerv.battle.boot.module.base.repository.mapper.PermissionMapper;
import com.gnerv.battle.boot.module.base.repository.mapper.RolePermissionMapper;
import com.gnerv.battle.boot.module.base.model.bo.PermissionBO;
import com.gnerv.battle.boot.module.base.model.dto.PermissionDTO;
import com.gnerv.battle.boot.module.base.model.entity.Permission;
import com.gnerv.battle.boot.module.base.service.CurrentAccountService;
import com.gnerv.battle.boot.module.base.service.PermissionService;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

import static com.gnerv.battle.boot.common.tools.PageTools.limitSize;
import static com.gnerv.battle.boot.common.tools.PageTools.limitStart;

/**
 * @author Gnerv LiGen
 */
@Service
@AllArgsConstructor
public class PermissionServiceImpl implements PermissionService {

    private final PermissionMapper permissionMapper;
    private final RolePermissionMapper rolePermissionMapper;
    private final CurrentAccountService currentAccountService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean create(@NonNull PermissionBO permissionBO) {
        Permission permission = permissionBO.toCreatePermission();
        permission.setCreator(currentAccountService.accountUkId());
        permissionMapper.insert(permission);
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteByUkId(@NonNull String ukId) {
        permissionMapper.deleteByUkId(ukId);
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateByUkId(@NonNull PermissionBO permissionBO) {
        Permission permission = permissionBO.toUpdatePermission();
        permissionMapper.updateByUkId(permission);
        return true;
    }

    @Override
    @Transactional(readOnly = true)
    public PermissionDTO selectByUkId(@NonNull String ukId) {
        return permissionMapper.selectByUkId(ukId);
    }

    @Override
    public String selectValueByUrlAndType(@NonNull String url, @NonNull String type) {
        type = type.toUpperCase();
        return permissionMapper.selectValueByUrlAndType(url, type);
    }

    @Override
    @Transactional(readOnly = true)
    public List<PermissionDTO> listByName(String name, Integer page, Integer size) {
        return permissionMapper.listByName(name, limitStart(page, size), limitSize(size));
    }

    @Override
    public List<PermissionDTO> listByRoleUkId(@NonNull String roleUkId) {
        return permissionMapper.listByRoleUkId(roleUkId);
    }

    @Override
    @Transactional(readOnly = true)
    public boolean isExist(@NonNull String url, @NonNull String type, String ukId) {
        return permissionMapper.isExist(url, type, ukId) > 0;
    }

    @Override
    public boolean bindRole(@NonNull String permissionUkId, @NonNull Set<String> roleUkIdList) {
        rolePermissionMapper.insertPermissionRole(permissionUkId, roleUkIdList, currentAccountService.accountUkId());
        return true;
    }

    @Override
    public boolean unbindRole(@NonNull String permissionUkId) {
        rolePermissionMapper.deleteByPermissionUkId(permissionUkId);
        return true;
    }

    @Override
    public int countByName(String name) {
        return permissionMapper.countByName(name);
    }
}




